import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/theme/kq_theme_manager.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';
import 'package:flutter_widgets_pc/widgets/checkbox/round_checkbox.dart';

import '../../resources/kq_theme_colors.dart';

/// 可勾选的按钮
class KqCheckedButton extends StatefulWidget {
  /// 是否勾选
  final bool isChecked;

  /// 是否build时自动设置_checked=isChecked
  final bool updateCheckedWhenBuild;

  /// 选中时的文字
  final String checkedString;

  /// 不选中是的文字
  final String unCheckedString;

  /// 文字大小
  final double? fontSize;

  /// 文字颜色
  final Color? textColor;

  /// CheckBox 选中颜色
  final Color? checkedBoxColor;

  /// CheckBox 未选中颜色
  final Color? uncheckedBoxColor;

  /// CheckBox 边框颜色
  final Color? borderColor;

  /// CheckBox和文字之间的距离，默认6
  final double? spacing;

  /// CheckBox的圆角大小，默认2
  final double? boxRadius;

  /// 整体的水波纹圆角大小，默认2
  final double? buttonRadius;

  /// 整体背景颜色，默认透明
  final Color? backgroundColor;

  /// CheckBox的宽高，默认16
  final double? boxSize;

  /// CheckBox是否圆形，默认true
  final bool isRound;

  /// 水平左右padding，默认2
  final double? horizontalPadding;

  /// 垂直左右padding，默认2
  final double? verticalPadding;

  /// 选中改变监听
  final Function(bool checked)? onCheckedChanged;

  const KqCheckedButton(
      {Key? key,
      this.isChecked = false,
      required this.checkedString,
      required this.unCheckedString,
      this.onCheckedChanged,
      this.updateCheckedWhenBuild = false,
      this.backgroundColor,
      this.spacing,
      this.boxSize,
      this.borderColor,
      this.buttonRadius,
      this.checkedBoxColor,
      this.uncheckedBoxColor,
      this.isRound = true,
      this.horizontalPadding,
      this.verticalPadding,
      this.fontSize,
      this.textColor,
      this.boxRadius})
      : super(key: key);

  @override
  KqCheckedButtonState createState() => KqCheckedButtonState();
}

class KqCheckedButtonState extends State<KqCheckedButton> {

  late bool _checked;

  @override
  void initState() {
    _checked = widget.isChecked;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.updateCheckedWhenBuild) {
      _checked = widget.isChecked;
    }
    return KqInkWell(
      radius: widget.buttonRadius ?? 2,
      verticalPadding: widget.verticalPadding ?? 2,
      horizontalPadding: widget.horizontalPadding ?? 2,
      backgroundColor: widget.backgroundColor ?? KqThemeColors.bgTransparent,
      onTap: widget.onCheckedChanged == null
          ? null
          : () {
              setState(() {
                _checked = !_checked;
                widget.onCheckedChanged?.call(_checked);
              });
            },
      child: Row(
        children: [
          RoundCheckBox(
              size: widget.boxSize ?? 16,
              isChecked: _checked,
              isRound: widget.isRound,
              roundRadius: widget.boxRadius,
              checkedColor: widget.checkedBoxColor ?? KqThemeManager.getCommonConfig()
                  .itemContentColor ??
                  KqThemeManager.getCommonConfig().mainLightColor,
              uncheckedColor: widget.uncheckedBoxColor,
              borderColor: widget.borderColor),
          SizedBox(
            width: widget.spacing ?? 6,
          ),
          Text(
            _checked ? widget.checkedString : widget.unCheckedString,
            style: TextStyle(
                fontSize: widget.fontSize ?? 14,
                color: widget.textColor ?? KqThemeColors.text26),
          ),
        ],
      ),
    );
  }
}
